﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.Utils.Win;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DevExpress.XtraGrid.Demos.util;

namespace DXApplicationTangche.UC.订单一览新.辅料.辅料管理
{
    public partial class Form辅料一览 : DevExpress.XtraEditors.XtraForm
    {
        private String accessoryCatalogId;
        private String accessoryCatalogName;
        public Form辅料一览()
        {
            InitializeComponent();
            this.searchLookUpEdit辅料类别.Properties.DataSource = 辅料Service.get辅料类别一览(false);
        }

        #region 选择辅料类别
        private void searchLookUpEdit辅料类别_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit辅料类别View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit辅料类别.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.accessoryCatalogId = this.searchLookUpEdit辅料类别.Properties.View.GetRowCellValue(rowHandle, "accessoryCatalogId").ToString();
                    this.accessoryCatalogName = this.searchLookUpEdit辅料类别.Properties.View.GetRowCellValue(rowHandle, "accessoryCatalogName").ToString();
                }
            }
            this.gridControl类别下辅料一览.DataSource = 辅料Service.get类别下辅料一览(this.accessoryCatalogId, 0, 10000);
            this.gridView类别下辅料一览.RefreshData();
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit辅料类别.ToolTip = null;
            searchLookUpEdit辅料类别.EditValue = null;
        }
        private void searchLookUpEdit辅料类别_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.accessoryCatalogName;
        }
        #endregion

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.accessoryCatalogId) || String.IsNullOrEmpty(this.accessoryCatalogName))
            {
                MessageBox.Show("选择辅料类别");
                return;
            }
            new Form辅料维护(
                this.accessoryCatalogId,
                this.accessoryCatalogName
                ).ShowDialog();
            this.gridControl类别下辅料一览.DataSource = 辅料Service.get类别下辅料一览(this.accessoryCatalogId, 0, 1000);
            this.gridView类别下辅料一览.RefreshData();
            //try
            //{
            //    this.picture = FileService.GetImageByUrl(URL.图片url + URL.备注图片路径 + this.name);
            //}
            //catch { }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.accessoryCatalogId) || String.IsNullOrEmpty(this.accessoryCatalogName))
            {
                MessageBox.Show("选择辅料类别");
                return;
            }
            new Form辅料维护(
                new DTO辅料一览(
                    this.accessoryCatalogId,
                    this.accessoryCatalogName,
                    FunctionHelper.GetgridViewValue(
                        this.gridView类别下辅料一览, this.gridView类别下辅料一览.FocusedRowHandle, "subMaterialSupplier"),
                    FunctionHelper.GetgridViewValue(
                        this.gridView类别下辅料一览, this.gridView类别下辅料一览.FocusedRowHandle, "subMaterialNumber"),
                    FunctionHelper.GetgridViewValue(
                        this.gridView类别下辅料一览, this.gridView类别下辅料一览.FocusedRowHandle, "subMaterialCode"),
                    FunctionHelper.GetgridViewValue(
                        this.gridView类别下辅料一览, this.gridView类别下辅料一览.FocusedRowHandle, "subMaterialName"),
                    FunctionHelper.GetgridViewValue(
                        this.gridView类别下辅料一览, this.gridView类别下辅料一览.FocusedRowHandle, "subMaterialSpecDesc"),
                    FunctionHelper.GetgridViewValue(
                        this.gridView类别下辅料一览, this.gridView类别下辅料一览.FocusedRowHandle, "subMaterialPicture")
                    )
                ).ShowDialog();
            this.gridControl类别下辅料一览.DataSource = 辅料Service.get类别下辅料一览(this.accessoryCatalogId, 0, 1000);
            this.gridView类别下辅料一览.RefreshData();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                this.gridControl类别下辅料一览.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}